package com.rjpacket.supernews.app.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.rjpacket.supernews.app.MainActivity;
import com.rjpacket.supernews.app.NewsDetailActivity;
import com.rjpacket.supernews.app.R;
import com.rjpacket.supernews.app.models.News;
import com.rjpacket.supernews.app.models.NewsDetail;
import com.rjpacket.supernews.app.tasks.DetailTaskCallOnBack;
import com.rjpacket.supernews.app.tasks.RecommendNewsDetailTask;
import com.rjpacket.supernews.app.utils.DataContainer;
import com.rjpacket.supernews.app.utils.ImageLoader;
import com.rjpacket.supernews.app.utils.MyImageLoadMethod;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by aaa on 15-3-10.
 */
public class NewsFragmentAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<News> data;
    private News news;
    private Context context;
    private View.OnClickListener listener;

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    public NewsFragmentAdapter(Context context, List<News> data) {
        this.data = data;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取每一个item的数据
        news = data.get(position);

        //设置holder
        ViewHolder holder = null;
        //获取图片的网址的临时数据库
        DataContainer dc = DataContainer.getInstance();

        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    //加载无图片的布局
                    convertView = inflater.inflate(R.layout.rjp_item_news_0, parent, false);
                    holder = new ViewHolder(convertView);
                    break;
                case 1:
                    //加载一张图片的布局
                    convertView = inflater.inflate(R.layout.rjp_item_news_1, parent, false);
                    holder = new ViewHolder(convertView);
                    holder.rightPic = (ImageView) convertView.findViewById(R.id.news_one_pic);
                    break;
                case 2:
                    //加载两张图片的布局
                    convertView = inflater.inflate(R.layout.rjp_item_news_3, parent, false);
                    holder = new ViewHolder(convertView);
                    holder.one = (ImageView) convertView.findViewById(R.id.news_three_pic_1);
                    holder.two = (ImageView) convertView.findViewById(R.id.news_three_pic_2);
                    holder.three = (ImageView) convertView.findViewById(R.id.news_three_pic_3);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setTag(position);
        holder.leftLinear.setTag(position);
        holder.rightLinear.setTag(position);

        String title = news.getTitle();

        holder.title.setText(title);

        holder.zan.setText(news.getComment_num() + "");
        holder.source.setText(news.getSource());
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        holder.time.setText(sdf.format(news.getTime()));

        holder.newsType.setVisibility(View.INVISIBLE);

        int nmark = news.getNmark();
        if (nmark == 1) {
            holder.hotRecommend.setImageResource(R.drawable.ic_mainlist_hot);
        } else if (nmark == 2) {
            holder.hotRecommend.setImageResource(R.drawable.ic_mainlist_recommend);
        } else {
            holder.hotRecommend.setVisibility(View.INVISIBLE);
        }

        if (holder.rightPic != null) {
            holder.rightPic.setImageResource(R.drawable.atlas_load_pic);
            ArrayList<String> thumbs = news.getThumbs();

            MyImageLoadMethod.load(dc.getBaseUrl() + thumbs.get(0), holder.rightPic);

        }

        if (holder.one != null && holder.two != null && holder.three != null) {
            holder.one.setImageResource(R.drawable.atlas_load_pic);
            holder.two.setImageResource(R.drawable.atlas_load_pic);
            holder.three.setImageResource(R.drawable.atlas_load_pic);

            ArrayList<String> thumbs = news.getThumbs();

            MyImageLoadMethod.load(dc.getBaseUrl() + thumbs.get(0), holder.one);
            MyImageLoadMethod.load(dc.getBaseUrl() + thumbs.get(1), holder.two);
            MyImageLoadMethod.load(dc.getBaseUrl() + thumbs.get(2), holder.three);

        }
        return convertView;
    }


    private class ViewHolder implements View.OnClickListener {
        public TextView title;
        public TextView zan;
        public TextView source;
        public TextView time;


        public ImageView hotRecommend;
        public ImageView newsType;
        public ImageView down;

        public ImageView rightPic;

        public ImageView one;
        public ImageView two;
        public ImageView three;

        public View view;

        public View leftLinear;
        public View rightLinear;

        public ViewHolder(View itemView) {
            title = (TextView) itemView.findViewById(R.id.news_title);

            zan = (TextView) itemView.findViewById(R.id.news_zan);
            source = (TextView) itemView.findViewById(R.id.news_source);
            time = (TextView) itemView.findViewById(R.id.news_time);

            hotRecommend = (ImageView) itemView.findViewById(R.id.news_hot);
            newsType = (ImageView) itemView.findViewById(R.id.news_type);
            down = (ImageView) itemView.findViewById(R.id.news_down_btn);
            down.setOnClickListener(this);

            view = itemView.findViewById(R.id.common_save_favorite);

            title.setOnClickListener(listener);

            leftLinear = itemView.findViewById(R.id.list_item_save);
            leftLinear.setOnClickListener(listener);
            rightLinear = itemView.findViewById(R.id.list_item_delete);
            rightLinear.setOnClickListener(listener);
        }

        @Override
        public void onClick(View v) {
            int visibility = view.getVisibility();
            if (visibility == View.GONE) {
//                Animation anim1 = AnimationUtils.loadAnimation(context, R.anim.act_linear_top_in);
//                view.setAnimation(anim1);
//                anim1.start();
                view.setVisibility(View.VISIBLE);
            } else {
//                Animation anim2 = AnimationUtils.loadAnimation(context, R.anim.act_linear_top_out);
//                view.setAnimation(anim2);
//                anim2.start();
                view.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        News news = data.get(position);

        if (news.getType() == News.Type.ONE) {
            return 1;
        } else if (news.getType() == News.Type.ZERO) {
            return 0;
        }
        return 2;

    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }
}
